<?php
declare (strict_types=1);

namespace app\admin\service\data;

use app\common\service\BaseService;
use think\facade\Request;

/**
 * 数据类接口默认基类
 */
class DataService extends BaseService
{
    /**
     * 当前的查询时间字符串
     *
     * @var [type]
     */
    protected $timestr;

    public function __construct()
    {
        $this->timestr = Request::get('time/s', 'today');
    }

    /**
     * 解析时间
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-08
     *
     * @return   array
     */
    protected function parseTime()
    {
        $day = '';
        switch ($this->timestr) {
            case 'week':
                // 近一周
                $day = '-7 days';
                break;
            case 'month':
                // 近一月
                $day = '-30 days';
                break;
            default:
                // 今天
                $day = date('Y-m-d');
                break;
        }

        return [strtotime($day), Request::time()];
    }

    /**
     * 解析点击选择的固顶日期
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @return   [type]                         [description]
     */
    protected function parseDate($time = null, $starttime = 0)
    {
        $days = 1;
        $date = [];
        if (is_numeric($time)) {
            $days = $time;
        } else {
            $time = is_null($time) ? Request::get('time/s', 'today') : $time;
            switch ($time) {
                case 'week':
                    // 最近7天
                    $days = 7;
                    break;
                case 'month':
                    // 最近30天
                    $days = 30;
                    break;
                case 'today':
                default:
                    // 当天
                    $days = 1;
                    break;
            }
        }
        if ($days == 1) {
            $starttime = $starttime ?: time();
            $h         = date('Y-m-d', $starttime) != date('Y-m-d') ? 23 : date('H');

            for ($i = 0; $i <= $h; $i++) {
                $timestr        = str_pad((string)$i, 2, '0', STR_PAD_LEFT) . ":00";
                $start          = strtotime(date('Y-m-d ', $starttime) . $timestr);
                $date[$timestr] = [$start, $start + 3599];
            }
        } else {
            while ($days > 0) {
                $days--;
                $start                       = strtotime('-' . $days . ' days');
                $start                       = strtotime(date('Y-m-d', $start));
                $date[date('Y-m-d', $start)] = [$start, $start + 86399];

            }
        }

        return $date;
    }

    /**
     * 解析选择的时间区间
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     *
     * @return   [type]                         [description]
     */
    protected function parseSelectDaterange()
    {
        $time = Request::get('daterange/a', [date('Y-m-d'), date('Y-m-d')]); // 拆分起始时间
        [$start, $end] = $time;
        // 起始时间转换为时间戳
        $start = strtotime(date('Y-m-d', strtotime($start)));
        $end   = strtotime(date('Y-m-d', strtotime($end)));
        // 计算相差天数
        $days = ($end - $start) / 86400 + 1;

        return [
            'start'    => $start,
            'end'      => $end + 86399,
            'timespan' => [$start, $end + 86399],
            'days'     => $days,
            'rows'     => $this->parseDate($days, $start),
        ];
    }

    /**
     * 计算占比
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-20
     *
     * @param    [type]                         $total      [description]
     * @param    [type]                         $proportion [description]
     * @param integer $default 默认占比,当总数为0时
     * @return   [type]                                     [description]
     */
    protected function getProportionRate($total, $proportion, $default = 100)
    {

        $result = $total ? round(($proportion / $total) * 100, 2) : $default;

        return $result > 100 ? 100 : $result;
    }

    /**
     * 解析统计图日期维度
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     *
     * @param    [type]                         $rows [description]
     * @return   [type]                               [description]
     */
    protected function parseRowsDate($rows, $appends = [])
    {
        $dates = [];
        foreach ($rows as $key => $value) {
            $dates[] = array_merge($appends, ['date' => $key]);
        }

        return $dates;
    }

}
